package dianmobile.eistarandroid.ui.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import dianmobile.eistarandroid.R;
import dianmobile.eistarandroid.beans.ArticleDetailEntity;
import dianmobile.eistarandroid.network.request.RequestFactory;
import dianmobile.eistarandroid.ui.activities.PostArticleActivity;
import dianmobile.eistarandroid.ui.activities.UserInfoActivity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.DataUtils;
import dianmobile.eistarandroid.utils.HandleString;

/**
 * Created by Administrator on 2015/9/2.
 */
public class ViewPostListAdapter extends BaseAdapter {

    private ArticleDetailEntity originalArticle = new ArticleDetailEntity();
    private String replyNum;
    private List<ArticleDetailEntity> articleList;
    private LayoutInflater inflater;
    private Context context;
    private String boardName;

    final static int TYPE_ARTICLE_DETAIL = 0;
    final static int TYPE_INTERVAL = 1;
    final static int TYPE_REPLY = 2;

    /**
     *
     * @param context context
     * @param list 回复列表
     */
    public ViewPostListAdapter(Context context, List<ArticleDetailEntity> list, String boardName){

        this.articleList = list;
        this.context = context;
        this.boardName = boardName;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return articleList.size()+1;
    }

    @Override
    public Object getItem(int position) {
        return articleList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position){
        if (position == 0) {
            return TYPE_ARTICLE_DETAIL;
        } else if(position == 1){
            return TYPE_INTERVAL;
        } else {
            return TYPE_REPLY;
        }
    }
    @Override
    public int getViewTypeCount(){
        return 3;
    }

    private void getUserHeadFromNetwork(ImageView imageView, String imageName ){
        String url = DataUtils.getUserHeadUrlFromName(imageName);
        RequestFactory.makeNetworkImage(imageView, url, R.drawable.default_face, 0);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(articleList.size()>0) {
            this.originalArticle = articleList.get(0);
        }
        if((articleList.size()-1) <= 0) {
            this.replyNum = "共 0 篇";
        } else {
            this.replyNum = "共 " + (articleList.size()-1) + " 篇";
        }

        ViewHolderArticleDetail viewHolderArticleDetail = null;
        ViewHolderInterval viewHolderInterval = null;
        ViewHolderReply viewHolderReply = null;

        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case TYPE_ARTICLE_DETAIL:
                    convertView = inflater.inflate(R.layout.listitem_viewpost_article_detail, null);
                    viewHolderArticleDetail = new ViewHolderArticleDetail();
                    viewHolderArticleDetail.authorHead = (CircleImageView)convertView.findViewById(R.id.iv_author_head);
                    viewHolderArticleDetail.authorName = (TextView)convertView.findViewById(R.id.tv_author_name);
                    viewHolderArticleDetail.articleTitle = (TextView)convertView.findViewById(R.id.tv_article_title);
                    viewHolderArticleDetail.postTime = (TextView)convertView.findViewById(R.id.tv_post_time);
                    viewHolderArticleDetail.replyNum = (TextView)convertView.findViewById(R.id.tv_reply_num_article_detail);
                    viewHolderArticleDetail.articleDetail = (TextView)convertView.findViewById(R.id.tv_article_detail);

                    convertView.setTag(viewHolderArticleDetail);
                    break;
                case TYPE_INTERVAL:
                    convertView = inflater.inflate(R.layout.listitem_viewpost_interval,null);
                    viewHolderInterval = new ViewHolderInterval();
                    viewHolderInterval.replyNum = (TextView)convertView.findViewById(R.id.tv_reply_num_interval);

                    convertView.setTag(viewHolderInterval);
                    break;
                case TYPE_REPLY:
                    convertView = inflater.inflate(R.layout.listitem_viewpost_reply,null);
                    viewHolderReply = new ViewHolderReply();
                    viewHolderReply.replyAuthorHead = (CircleImageView)convertView.findViewById(R.id.iv_reply_author_head);
                    viewHolderReply.replyAuthorName = (TextView)convertView.findViewById(R.id.tv_reply_author_name);
                    viewHolderReply.replyMenu = (ImageButton)convertView.findViewById(R.id.btn_reply_menu);
                    viewHolderReply.replyTime = (TextView)convertView.findViewById(R.id.tv_reply_time);
                    viewHolderReply.replyDetail = (TextView)convertView.findViewById(R.id.tv_reply_detail);

                    convertView.setTag(viewHolderReply);
                    break;
                default:
                    break;
            }
        } else {
            switch (type) {
                case TYPE_ARTICLE_DETAIL:
                    viewHolderArticleDetail = (ViewHolderArticleDetail)convertView.getTag();
                    break;
                case TYPE_INTERVAL:
                    viewHolderInterval = (ViewHolderInterval)convertView.getTag();
                    break;
                case TYPE_REPLY:
                    viewHolderReply = (ViewHolderReply)convertView.getTag();
                    break;
                default:
                    break;
            }
        }

        switch (type) {
            case TYPE_ARTICLE_DETAIL:
                viewHolderArticleDetail.authorName.setText(originalArticle.getAuthorName());
                viewHolderArticleDetail.articleTitle.setText(originalArticle.getArticleTitle());
                viewHolderArticleDetail.replyNum.setText(replyNum);
                viewHolderArticleDetail.postTime.setText(HandleString.getTimeDifference
                        (originalArticle.getPostTime()));
                viewHolderArticleDetail.articleDetail.setText(HandleString.beautifulContent(originalArticle.getArticleDetail()));
                getUserHeadFromNetwork(viewHolderArticleDetail.authorHead, originalArticle.getAuthorName());
//                viewHolderArticleDetail.authorHead.setImageResource(R.drawable.default_face);
                viewHolderArticleDetail.authorHead.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra(ConstantsData.EXTRA_USER_NAME, originalArticle.getAuthorID());
                        context.startActivity(intent);
                    }
                });
                break;
            case TYPE_INTERVAL:
                viewHolderInterval.replyNum.setText(replyNum);
                break;
            case TYPE_REPLY:
                /**
                 * position - 1: 原文和中间间隔占2个位置，要减2，
                 * 但是评论是从列表的第一项开始，第0项是原文，要加1，所以是减1
*/                final ArticleDetailEntity replyArticle = articleList.get(position - 1);
                viewHolderReply.replyAuthorName.setText(replyArticle.getAuthorName());
                viewHolderReply.replyTime.setText(HandleString.getTimeDifference
                        (replyArticle.getPostTime()));
                viewHolderReply.replyDetail.setText(HandleString.beautifulContent(replyArticle.getArticleDetail()));
                getUserHeadFromNetwork(viewHolderReply.replyAuthorHead, replyArticle.getAuthorName());

                viewHolderReply.replyAuthorHead.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(context, UserInfoActivity.class);
                        intent.putExtra(ConstantsData.EXTRA_USER_NAME, replyArticle.getAuthorID());
                        context.startActivity(intent);
                    }
                });
                viewHolderReply.replyMenu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent intent = new Intent(context, PostArticleActivity.class);
                        intent.putExtra(ConstantsData.EXTRA_POST_TYPE, ConstantsData.POST_TYPE_REPLY);
                        intent.putExtra(ConstantsData.EXTRA_BOARD_NAME, boardName);
                        intent.putExtra(ConstantsData.REPLY_TITLE, articleList.get(0).getArticleTitle());
                        intent.putExtra(ConstantsData.EXTRA_FILE_NAME, replyArticle.getFileName());
                        intent.putExtra(ConstantsData.REPLY_SUMMARY,
                                "【 在 "+replyArticle.getAuthorName()+" 大作中提到: 】\n" + HandleString.getSummary(replyArticle.getArticleDetail()));
                        ((Activity)context).startActivityForResult(intent, 2);
                    }
                });
                break;
            default:
                break;
        }
        return convertView;
    }



    class ViewHolderArticleDetail {
        CircleImageView authorHead;
        TextView articleTitle;
        TextView authorName;
        TextView replyNum;
        TextView postTime;
        TextView articleDetail;
    }
    class ViewHolderInterval {
        TextView replyNum;
    }
    class ViewHolderReply {
        CircleImageView replyAuthorHead;
        TextView replyAuthorName;
        TextView replyTime;
        ImageButton replyMenu;
        TextView replyDetail;
    }
}
